package com.dykyi.SimpleIteration;
public class Calculation {

	private double sum = 0;
	private double[][] a1 = null;
	private double[] b1 = null;
	//method to check convergense of SLAR
	public boolean checkConvergense(double a[][], int kill) {
		for (int i = 0; i < kill; i++) {
			sum = 0;
			for (int j = 0; j < kill; j++) {
				if (i != j) {
					sum += Math.abs(a[i][j]);
				}
				if (j == kill - 1) {
					if (sum >= Math.abs(a[i][i])) {
						return false;
					}
				}
			}
		}
		return true;
	}
	
	////method to resolving SLAR
	public double[] resolvSlar(double a[][], double b[][], int kill, double accur) {
		
		double[] x = null;
		x = new double[kill];
		double[] x0 = null;
		x0 = new double[kill];
		a1 = new double[kill][kill];
		b1 = new double[kill];
		double max = 0;
		double sum = 0;
		int k = 0;
		
		// normalization of matrix
		for (int i = 0; i < kill; i++) {
			for (int j = 0; j < kill; j++) {
				if (i != j) {
					a1[i][j] = -(a[i][j] / a[i][i]);
				}
			}
			b1[i] = b[i][0] / a[i][i];
			a1[i][i] = 0;
			x0[i] = b1[i];
		}
	
		//resolving SLAR
		do {
			max = 0;
			for (int i = 0; i < kill; i++) {
				sum = 0;
				for (int j = 0; j < kill; j++) {
					sum = sum + a1[i][j] * x0[j];
				}
				x[i] = b1[i] + sum;
			}
			for (int i = 0; i < kill; i++) {
				if (Math.abs(x[i] - x0[i]) > max) {
					max = Math.abs(x[i] - x0[i]);
				}
				x0[i] = x[i];
			}
			k++;
		}
		while ((k > 1000) || (max < accur));
		//returning matrix of results
		return x;
	}
}
